<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class MoneyController extends Controller
{
    /*获取单价*/
    public static function getMoney($request){
        if ($request->isMethod('post')) {
            try{
                $params = $request->only('tool_id','std_id','vol_id','class_id','items');
                $items = $params['items'];
                $moneys = []; //0：额定电压；1：分类
                $param = [
                        'tool_id'=>$params['tool_id'],
                        'std_id'=>$params['std_id'],
                    ];
                foreach ($items as $item_id) {
                    $param['item_id'] = $item_id;
                    //查询vol_id 并取值
                    $param['vol_class_id'] = $params['vol_id'];
                    $money = DB::table('a_money')->where($param)->value('val');
                    $moneys[0][$item_id] = $money;
                    //查询class_id 并取值
                    $param['vol_class_id'] = $params['class_id'];
                    $money = DB::table('a_money')->where($param)->value('val');
                    $moneys[1][$item_id] = $money;
                }
            }catch(\Exception $e){
                return 0;
            }
            return $moneys;
        }
    }
    /*编辑，修改价格*/
    public function edit(Request $request){
        if($request->isMethod('post')){
            $data = $request->data;
            if ($data) { //如果是多数据提交
                $count = 0;
                foreach($data as $item){
                    $res = $this->editData($item);
                    $count += $res ? 1 : 0;
                }
                return $count;
            }
            $params = $request->only('id','val','remark','tool_id','std_id','vol_class_id','item_id');
            return $this->editData($params);
        }
    }
    private function editData($params){
        $id = $params['id'];
        unset($params['id']);
        if ($id) {
            $res = DB::table('a_money')->where('id',$id)->update($params);
            return $res ? 1 : 0;
        }else{
            $res = DB::table('a_money')->insertGetId($params);
            return $res ?: 0;
        }
        return 0;
    }
    /*列表*/
    public function index(Request $request)
    {
        $tools = DB::table('a_tool')->where('ishide',0)->pluck('name','id')->toArray();
        //工器具-》标准-》额定电压、分类-》项目
        $moneys = DB::table('a_money')->get()->keyBy(function($item){
            return $item->tool_id.'-'.$item->std_id.'-'.$item->item_id.'-'.$item->vol_class_id;
        })->toArray();
        $volClass = DB::table('a_voltage_class')
            ->select('id','std_id','tool_id','val')
            ->where('ishide',0)
            ->where('id','!=',0)
            ->orderBy('id','asc')
            ->get();
        $volClass2 = [];
        foreach($volClass as $item){
            $volClass2[$item->tool_id.'-'.$item->std_id][] = $item;
        }
        //每个工器具下面的检测标准对应的试验项目
        $itemRels = DB::table('a_std_tool_item_rel as a')
            ->leftJoin('a_tool as b','a.tool_id','=','b.id')
            ->leftJoin('a_testitem as c','a.testitem_id','=','c.id')
            ->leftJoin('a_standard as d','a.std_id','=','d.id')
            ->select('a.std_id','a.tool_id','a.testitem_id as item_id','b.name as tool_name','c.name as item_name','d.num as std_num')
            ->where('a.ishide',0)
            ->orderBy('b.name','asc')
            ->orderBy('d.num','asc')
            ->orderBy('c.name','asc')
            ->get();
        $data = [];
        foreach ($itemRels as  $itemRel) {
            $k = $itemRel->tool_id.'-'.$itemRel->std_id;
            $items = array_get($volClass2,$k,[]);
            if($items){ //有额定电压、分类的情况：
                foreach ($items as $item) {
                    $money = array_get($moneys,$k.'-'.$itemRel->item_id.'-'.$item->id,'');
                    $itemRel->money_id = data_get($money,'id',0);
                    $itemRel->vol_class_id = $item->id;
                    $itemRel->vol_class_val = $item->val;
                    $itemRel->val = data_get($money,'val','');
                    $itemRel->remark = data_get($money,'remark','');
                    $data[] = (object)(array)$itemRel; //直接对象的话会覆盖之前的，不知道为什么
                }
            }else{ //没有额定电压、分类的情况：
                $money = array_get($moneys,$k.'-'.$itemRel->item_id.'-'.data_get($itemRel,'vol_class_id',0),'');
                $itemRel->money_id = data_get($money,'id',0);
                $itemRel->vol_class_id = 0;
                $itemRel->vol_class_val = '';
                $itemRel->val = data_get($money,'val','');
                $itemRel->remark = data_get($money,'remark','');
                $data[] = $itemRel;
            }
        }
        $dataCounts = []; //总结相同数据可以合并的行数
        // foreach ($volClass as $k => $val){
        //     $tool_id = $val->tool_id;
        //     $std_id = $val->std_id;
        //     $vol_class_id = $val->vol_class_id;
        //     $dataCountItems = [
        //         $tool_id,
        //         $tool_id.'.'.$std_id,
        //         $tool_id.'.'.$std_id.'.'.$vol_class_id,
        //     ];
        //     $items = array_get($itemRels,$tool_id.'.'.$std_id,[]);
        //     foreach($items as $item){
        //         foreach($dataCountItems as $dataCountItem){
        //             array_set($dataCounts,$dataCountItem.'.count',array_get($dataCounts,$dataCountItem.'.count',0)+1);
        //         }
        //     }
        // }
        // dd($dataCounts);
        // dd($volClass);
        // dd($itemRels);
        return view('admin.money.index',compact('data','dataCounts','tools'));
    }
}
